package jj.yy.com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class Demo {
	//创建仓库
	private HashMap<String, Integer> w1 = new HashMap<>();
	private HashMap<String, Integer> w2 = new HashMap<>();
	private HashMap<String, Integer> w3 = new HashMap<>();
	//总仓库
	private ArrayList<HashMap<String, Integer>> w = new ArrayList<>();
	//仓库名集合
	private String[] wNames = new String[]{"w1", "w2", "w3"};
	//商品名集合
	private String[] Names = {"A", "B", "C"};
	
	
	/*
	 * @param name ,number
	 * 
	 */
	public Demo(String name, Integer number) {
		
		//判断商品是否存在
		int a = 0;
		for (String s : Names) {
			if (s.equals(name)) {
				break;
			}
			++a;
		}
		if (a == 3) {
			System.out.println("抱歉，商品不存在！");
			return;
		}
		//保证用户购买商品数合法
		if (number <= 0) {
			System.out.println("非法数据！");
			return;
		}
		//填充仓库数据
		w1.put("wType", 0);
		w1.put("A", 1);
		w1.put("B", 2);
		w1.put("C", 2);
		w1.put("distance", 1);
		w1.put("priority", 1);
		
		w2.put("wType", 1);
		w2.put("A", 2);
		w2.put("B", 1);
		w2.put("C", 2);
		w2.put("distance", 2);
		w2.put("priority", 2);
		
		w3.put("wType", 2);
		w3.put("A", 1);
		w3.put("B", 3);
		w3.put("C", 1);
		w3.put("distance", 2);
		w3.put("priority", 3);
		w.add(w1);
		w.add(w2);
		w.add(w3);
		//获取权限：商品数量权限>距离权限>权重权限
		Collections.sort(w, new Comparator<HashMap<String, Integer>>() {
			@Override
			public int compare(HashMap<String, Integer> o1, HashMap<String, Integer> o2) {
				//商品数量权限
				int temp =  o2.get(name).compareTo(o1.get(name));
				if (temp == 0) {
					//距离权限
					temp = o1.get("distance").compareTo(o2.get("distance"));
					if (temp == 0) {
						//权重权限
						return o2.get("prioriry").compareTo(o1.get("prioriry"));
					}
				}
				return temp;
			}
		});
		//获取单一商品的总数
		int sum = 0;
		for (HashMap<String, Integer> m : w) {
			sum += m.get(name);
		}
		if (number > sum) {
			System.out.println("抱歉，库存不足！");
			return;
		}
		System.out.println("发货单号\t商品名称\t数量\t出货仓库");
		boolean flag = true;
		//若该仓库商品数大于顾客所需数，输出顾客所需数量
		//若小于，将该仓库商品的全部取出，并用顾客所需数减该仓库商品数
		for (int i = 0; i < w.size() && flag; ++i) {
			int num = w.get(i).get(name);
			int temp = num;
			if (num >= number) {
				flag = false;
				temp = number;
			} else {
				number -= num;
			}
			System.out.format("%d\t%s\t%d\t%s%n", 
					i + 1,
					name,
					temp,
					wNames[w.get(i).get("wType")]);
		}
		System.out.println("---------------------------");
		
	}
}
